package top.lyc829.mymusic.ext

import android.util.Log
import androidx.lifecycle.viewModelScope
import com.lzx.starrysky.utils.showToast
import kotlinx.coroutines.launch
import top.lyc829.mymusic.MyApp
import top.lyc829.mymusic.base.BaseViewModel
import top.lyc829.mymusic.util.Const

/**
 * @Author lyc
 * @create 2021-07-06 15:12
 */
fun BaseViewModel.launch(block: suspend () -> Unit) = viewModelScope.launch {
    try {
        isLoading.value = true
        block()
        isLoading.value = false
    } catch (t: Throwable) {
        t.printStackTrace()
        //Toast.makeText(MyApp.context, t.message, Toast.LENGTH_SHORT).show()
        MyApp.context.showToast(t.message)
        t.message?.let { Log.e(Const.NET_TAG, it) }
        isLoading.value = false
    }
}